﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace MyDefaults.Core
{
    public class RegistryHelper
    {
        public void ApplyChanges(IList<RegistryEntry> settings)
        {
            foreach (RegistryEntry entry in settings)
            {
                if (entry.KeyName.StartsWith("HKEY_CURRENT_USER\\"))
                {
                    if (entry.ValueKind == RegistryValueKind.String)
                    {
                        SetCurrentUserKey(entry.KeyName.Remove(0, 18), entry.ValueName, (string)entry.Value);
                    }
                    // Yes, I know this is totally incomplete. I only needed strings...
                }
            }
        }

        private static void SetCurrentUserKey(string keyName, string valueName, string value)
        {
            try
            {
                RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true);

                if (key != null && key.GetValueKind(valueName) == RegistryValueKind.String)
                {
                    key.SetValue(valueName, value);
                }
            }
            catch (Exception e)
            {
            }
        }
    }
}
